import json

from django.http import Http404
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView,ListCreateAPIView
from rest_framework import status
from rest_framework import mixins
from booktest.models import BookInfo
from booktest.serializers import BookInfoSerializer
# Create your views here.

class MyListCreateModelView(mixins.ListModelMixin,
                            mixins.CreateModelMixin,
                            GenericAPIView):
    def get(self,request):
        return self.list(request)
    def post(self,request):
        return self.create(request)
# /books/
class BookListView(MyListCreateModelView):
    # 指定当前视图所使用的序列化器类
    serializer_class = BookInfoSerializer
    # 指定当前视图所使用的查询集
    queryset = BookInfo.objects.all()


class MyRetrieveUpdateDestroAPIView(mixins.RetrieveModelMixin,
                                    mixins.UpdateModelMixin,
                                    mixins.DestroyModelMixin,
                                    GenericAPIView):
    def get(self,request,*args,**kwargs):
        return self.retrieve(request)
    def put(self,request,*args,**kwargs):
        return self.update(request)
    def delete(self, request, *args, **kwargs):
        return self.destroy(request)
    
# /books/(?P<pk>\d+)/
# /books/9/
class BookDetailView(MyRetrieveUpdateDestroAPIView):
    # 指定当前视图所使用的序列化器类
    serializer_class = BookInfoSerializer
    # 指定当前视图所使用的查询集
    queryset = BookInfo.objects.all()


